
Page({
  data: {
    // 当前选择的导航字母
    selected: 0,
    // 选择字母视图滚动的位置id
    scrollIntoView: 'A',
    // 导航字母
    letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z'],
    groups: [],  //存放返回的植物的信息
  },
  onLoad: function (options) {
    //处理数据函数
    this.dealWithData();
  },
  //读取点击的字母
  tabLetter(e) {
    // console.log(e);
    const index = e.currentTarget.dataset.index;
    this.setData({
      selected: index,
      scrollIntoView: index
    })
    console.log(index);
    this.cleanAcitvedStatus();
  },
  // 清除字母选中状态
  cleanAcitvedStatus() {
    setTimeout(() => {
      this.setData({
        selected: 0
      })
    }, 500);
  },
  //触摸事件
  touchend(e) {
    this.cleanAcitvedStatus();
  },
  //处理返回的数据
  dealWithData: function () {
    var that = this;
    var searchKey = 'forageplant';
    var keData;
    var groups = [];
    var nameStr;//关键字数组
    var singleItem = {};//首字母的算一个项目
    var length = 0;//关键字的长度
    var i = 0;//循环控制变量
    var j = 0;
    var userLength = 0;//搜字母相同的全部牧草/蜜源的长度
    var nameData = {};//一个牧草/蜜源对应一个对象
    var keDataName = [];//搜字母相同的全部牧草/蜜源
    var userData = [];//搜字母相同的全部牧草/蜜源的对应的nameData的集合
    var keShuUrl = getApp().globalData.epUrl + searchKey;
    console.log(keShuUrl);
    wx.request({
      url: keShuUrl,
      success: function (res) {
        keData = res.data;
        console.log(keData);
        nameStr = keData['keyStr'];
        length = nameStr.length;
        console.log('关键字的长度' + length);
        for (i = 0; i < length; i++) {
          singleItem = {};
          singleItem['groupName'] = nameStr[i];
          userData = [];
          keDataName = keData[nameStr[i]];
          userLength = keDataName.length;
          for (j = 0; j < userLength; j++) {
            nameData = {};
            nameData['name'] = keDataName[j];
            userData.push(nameData);
          }
          singleItem['users'] = userData;
          groups.push(singleItem);
          console.log(groups);
          that.setData({ groups: groups });
        }
      }
    })
  },
  //点击进入搜索按钮
  searchButton: function (e) {
    var searchKey = e.currentTarget.dataset.text;
    console.log(e);
    var plantSearchUrl = getApp().globalData.plantSearch + searchKey;
    console.log(plantSearchUrl);
    wx.request({
      url: plantSearchUrl,
      success: function (res) {
        var returnData = res.data;
        console.log(returnData);
        wx.navigateTo({
          url: "../../searchinfo/searchinfo"
        }),
          wx.setStorageSync('returnData', returnData);
        return;
      }
    })
  }
})